//
//  VisitorView.swift
//  01- 搭建框架
//
//  Created by as888 on 15/10/16.
//  Copyright © 2015年 任伟. All rights reserved.
//

import UIKit
import SnapKit
///访客视图 － 处理用户未登录的界面显示
class VisitorView: UIView {
    
    /// MARK: - 设置视图信息
    func setupInfo(imageName: String?, title: String) {
        
        messageLabel.text = title
        
        // 如果图片名称为 nil，说明是首页，直接返回
        guard let imgName = imageName else {
            
            //播放动画
            startAnim()
            
            return
        }
        
        iconView.image = UIImage(named: imgName)
        // 隐藏小房子
        homeIconView.hidden = true
        // 将遮罩图像移动到底层
        sendSubviewToBack(maskIconView)
    }
    
    ///开启首页的动画
    private func startAnim() {
    
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        
        anim.removedOnCompletion = false
        
        iconView.layer.addAnimation(anim, forKey: nil)
        
        
    
    }
    
    /// MARK: - 构造函数
    // initWithFrame 是 UIView 的指定的构造函数
    // 使用纯代码开发的时候使用
    override init(frame: CGRect) {
        super.init(frame: frame)
    
        setupUI()
    }

    //initWithCoder 是使用SB & XIB 开发加载函数
    //是 SB 开始的入口
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        setupUI()
    }
    
    // MARK: - 懒加载控件
    private lazy var iconView: UIImageView = UIImageView.init(imageName: "visitordiscover_feed_image_smallicon")
    
    private lazy var maskIconView: UIImageView = UIImageView.init(imageName: "visitordiscover_feed_mask_smallicon")
    
    private lazy var homeIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var messageLabel: UILabel = UILabel.init(title: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜")
   // lazy var registerButton: UIButton = UIButton.init(title: "注销", fontSize: 17, color: UIColor.orangeColor(), imageName: "common_button_white_disable")
    
    lazy var registerButton: UIButton = UIButton.init(title: "注销", color: UIColor.orangeColor(), imageName: "common_button_white_disable")
    
    //lazy var loginButton: UIButton = UIButton.init(title: "登录", fontSize: 17, color: UIColor.orangeColor(), imageName: "common_button_white_disable")
    lazy var loginButton: UIButton = UIButton.init(title: "登录", color: UIColor.orangeColor(), imageName: "common_button_white_disable")
}

extension VisitorView {

    /// 设置界面(control + i 缩拍)
    private func setupUI() {
    
        //1. 添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        // 设置自动布局
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-60)
        }
       
        homeIconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView.snp_center)
        }
      
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
            make.width.equalTo(224)
            make.height.equalTo(36)
        }
        
        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(messageLabel.snp_left)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(36)
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLabel.snp_right)
            make.top.equalTo(registerButton.snp_top)
            make.width.equalTo(registerButton.snp_width)
            make.height.equalTo(registerButton.snp_height)
        }
       
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.bottom.equalTo(registerButton.snp_bottom)
        }
        
        //设置背景颜色（解决视图整体颜色的效果）
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
    }
    
}
